package dongshi.daddy.strategy.v2;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

public class PayTypeFactory {
    //    private static Map<Integer, PayAmountStrategy> productMap = new HashMap<>();
    private static Map<String, PayAmountStrategy> productMap = new HashMap<>();

    static {
        ServiceLoader<PayAmountStrategy> load = ServiceLoader.load(PayAmountStrategy.class);
        Iterator<PayAmountStrategy> iterator = load.iterator();
        while (iterator.hasNext()) {
            PayAmountStrategy next = iterator.next();
            Class<? extends PayAmountStrategy> aClass = next.getClass();
            if (!aClass.isAnnotationPresent(dongshi.daddy.strategy.v2.Key.class))
                throw new IllegalStateException("class: " + aClass + " expect @dongshi.daddy.strategy.v2.Key, but not found!");
//            String payType = aClass.getAnnotation(Key.class).payType();
            productMap.put(aClass.getAnnotation(Key.class).payType(), next);
        }
    }

    public static PayAmountStrategy makeProduct(String payType) {
        return productMap.get(payType);
    }
}
